import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:myapp/app/data/entities/article_item.dart';
import 'package:myapp/app/routes/app_pages.dart';
import 'package:myapp/app/theme/app_color.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../controllers/info_list_controller.dart';

class InfoListView extends StatelessWidget {
  final int position;

  InfoListView(this.position) {
    print("InfoListView con $position");
  }

  _buildTextItem(TextArticleItem item) {
    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            item.title,
            style: TextStyle(fontSize: 14, color: AppColor.hex_333),
          ),
          Padding(
            padding: EdgeInsets.only(top: 5),
            child: Text(
              "${item.source}  ${item.count}评论",
              style: TextStyle(fontSize: 12, color: AppColor.hex_666),
            ),
          )
        ],
      ),
    );
  }

  _buildImageItem(ImageArticleItem item) {
    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        children: [
          Expanded(
            child: Container(
              margin: EdgeInsets.only(right: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    item.title,
                    style: TextStyle(fontSize: 14, color: AppColor.hex_333),
                  ),
                  Text(
                    "${item.source}  ${item.count}评论  ",
                    style: TextStyle(fontSize: 12, color: AppColor.hex_666),
                  ),
                ],
              ),
              height: 80,
            ),
          ),
          SizedBox(
            width: 100,
            height: 80,
            child: ClipRRect(
              child: Image.network(
                item.img,
                fit: BoxFit.cover,
              ),
              borderRadius: BorderRadius.circular(5),
            ),
          ),
        ],
      ),
    );
  }

  _buildMultipleImageArticleItem(MultipleImageArticleItem item) {}

  Widget _buildItem(ArticleItem item) {
    if (item.type == 2) {
      return _buildImageItem(item as ImageArticleItem);
    } else if (item.type == 3) {
      return _buildMultipleImageArticleItem(item as MultipleImageArticleItem);
    } else {
      return _buildTextItem(item as TextArticleItem);
    }
  }

  _jump2Detail(InfoListController controller, int index) {
    Get.toNamed(Routes.ARTICLE_DETAILS,
        arguments: {"url": controller.getUrl(index)});
  }

  @override
  Widget build(BuildContext context) {
    final refreshController = RefreshController();
    return GetBuilder<InfoListController>(
      builder: (controller) {
        return Scaffold(
          body: Center(
            child: Obx(
              () => SmartRefresher(
                header: ClassicHeader(),
                onRefresh: controller.refresh,
                onLoading: controller.loading,
                controller: refreshController,
                child: ListView.builder(
                  itemBuilder: (context, index) {
                    return Column(
                      children: [
                        InkWell(
                            child: _buildItem(controller.list[index]),
                            onTap: () => _jump2Detail(controller, index)),
                        Divider(height: 1, color: AppColor.hex_ddd),
                      ],
                    );
                  },
                  padding: EdgeInsets.only(left: 20, right: 20),
                  itemCount: controller.length,
                ),
              ),
            ),
          ),
        );
      },
      init: InfoListController(),
    );
  }
}
